package com.project.demo.util;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.project.demo.dao.UserMapper;
import com.project.demo.entity.User;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
@Component
public class TokenUtils {
    @Resource
    private UserMapper userMapper;
    public User getUserIdByRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        String authorization = request.getHeader("x-auth-token");
        if(StringUtils.isNotBlank(authorization)){
            return userMapper.selectUserByToken(authorization);
        }
        return null;
    }
}
